package com.huawei.hwCloudJs.service.hms;

import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import com.huawei.appmarket.service.activitydispatcher.OpenGateway.Param;
import java.io.Serializable;
import java.util.Iterator;
import java.util.UUID;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class e {
    protected static final String a = "_val_type_";
    protected static final int b = -1;
    protected static final int c = 0;
    protected static final int d = 1;
    private static final String e = "_list_size_";
    private static final String f = "_list_item_";

    public static final class a extends Exception {
        public a(String str) {
            super(str);
        }
    }

    private Short a(JSONObject jSONObject) {
        Object opt = jSONObject.opt("value");
        if (opt == null) {
            return null;
        }
        if (opt instanceof Number) {
            return Short.valueOf(((Number) opt).shortValue());
        }
        if (!(opt instanceof String)) {
            return null;
        }
        try {
            return Short.valueOf(Short.parseShort((String) opt));
        } catch (NumberFormatException e) {
            return null;
        }
    }

    private void a(String str, JSONObject jSONObject, Bundle bundle) {
        JSONArray optJSONArray = jSONObject.optJSONArray("value");
        if (optJSONArray != null) {
            String optString = jSONObject.optString("subType");
            Bundle bundle2 = new Bundle();
            bundle2.putInt(a, 1);
            bundle2.putInt(e, optJSONArray.length());
            for (int i = 0; i < optJSONArray.length(); i++) {
                Object opt = optJSONArray.opt(i);
                if (opt != null) {
                    try {
                        JSONObject jSONObject2 = new JSONObject();
                        jSONObject2.put("value", opt);
                        jSONObject2.put("type", optString);
                        a(f + i, optString, jSONObject2, bundle2);
                    } catch (JSONException e) {
                        Log.e("JsonCodec", "writeList put json error");
                    }
                }
            }
            bundle.putBundle(str, bundle2);
        }
    }

    private Float b(JSONObject jSONObject) {
        Object opt = jSONObject.opt("value");
        if (opt == null) {
            return null;
        }
        if (opt instanceof Number) {
            return Float.valueOf(((Number) opt).floatValue());
        }
        if (!(opt instanceof String)) {
            return null;
        }
        try {
            return Float.valueOf(Float.parseFloat((String) opt));
        } catch (NumberFormatException e) {
            return null;
        }
    }

    private Long c(JSONObject jSONObject) {
        Object opt = jSONObject.opt("value");
        if (opt == null) {
            return null;
        }
        if (opt instanceof Number) {
            return Long.valueOf(((Number) opt).longValue());
        }
        if (!(opt instanceof String)) {
            return null;
        }
        try {
            return Long.valueOf(Long.parseLong((String) opt));
        } catch (NumberFormatException e) {
            return null;
        }
    }

    public Bundle a(JSONObject jSONObject, Bundle bundle) {
        Iterator keys = jSONObject.keys();
        while (keys.hasNext()) {
            String str = (String) keys.next();
            JSONObject optJSONObject = jSONObject.optJSONObject(str);
            if (optJSONObject != null) {
                a(str, optJSONObject.optString("type"), optJSONObject, bundle);
            }
        }
        return bundle;
    }

    public void a(String str, Bundle bundle, JSONObject jSONObject) {
        for (String str2 : bundle.keySet()) {
            Object obj = bundle.get(str2);
            if (obj != null) {
                try {
                    if (obj instanceof Bundle) {
                        Bundle bundle2 = (Bundle) obj;
                        int i = bundle2.getInt(a, -1);
                        if (i == 1) {
                            JSONArray jSONArray = new JSONArray();
                            a(str, jSONArray, bundle2);
                            jSONObject.put(str2, jSONArray);
                        } else if (i == 0) {
                            JSONObject jSONObject2 = new JSONObject();
                            a(str, bundle2, jSONObject2);
                            jSONObject.put(str2, jSONObject2);
                        }
                    } else if (obj instanceof Intent) {
                        r3 = str + "_" + UUID.randomUUID().toString() + "";
                        d.a().a(r3, (Intent) obj);
                        jSONObject.put(str2, r3);
                    } else if (obj instanceof PendingIntent) {
                        r3 = str + "_" + UUID.randomUUID().toString() + "";
                        d.a().a(r3, (PendingIntent) obj);
                        jSONObject.put(str2, r3);
                    } else {
                        jSONObject.put(str2, obj);
                    }
                } catch (JSONException e) {
                    Log.e("JsonCodec", "signInRes2Json put json error");
                }
            }
        }
    }

    protected void a(String str, String str2, JSONObject jSONObject, Bundle bundle) {
        if (jSONObject != null) {
            if (Param.TYPE_STR.equals(str2)) {
                bundle.putString(str, jSONObject.optString("value"));
            } else if ("Integer".equals(str2)) {
                bundle.putInt(str, jSONObject.optInt("value"));
            } else if ("Short".equals(str2)) {
                Short a = a(jSONObject);
                if (a != null) {
                    bundle.putShort(str, a.shortValue());
                }
            } else if ("Long".equals(str2)) {
                Long c = c(jSONObject);
                if (c != null) {
                    bundle.putLong(str, c.longValue());
                }
            } else if ("Float".equals(str2)) {
                Float b = b(jSONObject);
                if (b != null) {
                    bundle.putFloat(str, b.floatValue());
                }
            } else if ("Double".equals(str2)) {
                bundle.putDouble(str, jSONObject.optDouble("value"));
            } else if ("Boolean".equals(str2)) {
                bundle.putBoolean(str, jSONObject.optBoolean("value"));
            } else if ("List".equals(str2)) {
                a(str, jSONObject, bundle);
            } else if ("entity".equals(str2)) {
                JSONObject optJSONObject = jSONObject.optJSONObject("value");
                if (optJSONObject != null) {
                    Bundle a2 = a(optJSONObject, new Bundle());
                    a2.putInt(a, 0);
                    bundle.putBundle(str, a2);
                }
            }
        }
    }

    protected void a(String str, JSONArray jSONArray, Bundle bundle) throws JSONException {
        int i = bundle.getInt(e);
        for (int i2 = 0; i2 < i; i2++) {
            Object obj = bundle.get(f + i2);
            if (obj.getClass().isPrimitive() || (obj instanceof String) || (obj instanceof Serializable)) {
                jSONArray.put(obj);
            } else if (obj instanceof Bundle) {
                Bundle bundle2 = (Bundle) obj;
                if (bundle2.getInt(a, -1) == 0) {
                    JSONObject jSONObject = new JSONObject();
                    a(str, bundle2, jSONObject);
                    jSONArray.put(jSONObject);
                }
            }
        }
    }
}
